using System.Collections.Generic;
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;

namespace OhioState.Graphics.OpenGL
{

    public class ViewHandler : OpenGLObject, IView
    {
        #region IView interface
        public Viewport Viewport
        {
            get { return _viewProxy.Viewport; }
        }
        public ICamera CurrentCamera
        {
            get { return _viewProxy.CurrentCamera; }
        }
        public void AddLayer( ILayer layer )
        {
            // No-op
        }
        public void RemoveLayer( ILayer layer )
        {
            // No-op
        }
        private int _width = 128;
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private int _height = 128;
        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }
        public void CompositeLayers(OhioState.Graphics.IRenderPanel panel)
        {
            if (this.Invalid)
            {
                this.Invalid = false;
                UpdateView(panel);
            }

            if (Width != panel.Width || _height != panel.Height)
            {
                ResizeLayers(panel.Width, panel.Height);
            }
            
            _realView.CompositeLayers(panel);
        }

        private void UpdateView(OhioState.Graphics.IRenderPanel panel)
        {

            //
            // Copy over the viewport.
            //
            //debug!!!
            _realView.Viewport.Origin.x = 0;//_viewProxy.Viewport.Origin.x;
            _realView.Viewport.Origin.y = 0;//_viewProxy.Viewport.Origin.y;
            _realView.Viewport.Width = panel.Width;//_viewProxy.Viewport.Width;
            _realView.Viewport.Height = panel.Height;//_viewProxy.Viewport.Height;
            //
            // Rebuild list of internal LayerGL's. 
            //
            _realView.RemoveAllLayers();
            _layers.Clear();
            //
            // Access the list of layers in the ViewProxy.
            //
            foreach (ILayer layer in _viewProxy.Layers)
            {
                //
                // Not really a need to copy over the layer-proxy here.
                // We just want to inquire what layers to render from the
                //    OpenGL AssetManager.
                //

                //LayerGL layer = AssetManagerGL.Instance.GetItem(layerProxy) as LayerGL;
                IAssetManaged asset = layer as IAssetManaged;
                if (asset != null)
                {
                    ILayer layerGL = panel.ResourceManager.GetItem(asset) as ILayer;
                    if (layerGL != null)
                    {
                        _realView.AddLayer(layerGL);
                        _layers.Add(layerGL);
                        LayerHandler layerHandler = layerGL as LayerHandler;
                        //if (layerHandler != null)
                        //{
                        //    layerHandler.ResizePanel(Width, Height);
                        //}
                    }
                }
            }
        }

        private void ResizeLayers(int width, int height)
        {
            Width = width;
            Height = height;
            // Resize
            //foreach (ILayer layer in _layers)
            //{
            //    (layer as LayerHandler).ResizePanel(Width, Height);
            //}
        }
        #endregion IView interface

        //private OhioState.Controls.OpenGLPanel _glPanel;
        private ViewProxy _viewProxy;
        private OhioState.Graphics.OpenGL.ViewGL _realView;
        public ViewHandler( ViewProxy viewProxy )
            : base(viewProxy)
        {
            
            _viewProxy = viewProxy;
            //_viewProxy.AddChangedEventHandler(invalidateView);
            
            //
            // This needs to change later.
            //
            _realView = new ViewGL();
            //_width = _glPanel.Width;
            //_height = _glPanel.Height;
            this.Invalid = true;
        }
        //private void AddChangedEvent(IAssetManagedHandler e)
        //{
        //    _viewProxy.AddChangedEventHandler += e;
        //}
        private List<ILayer> _layers = new List<ILayer>(20);

        protected void invalidateView( IAssetManaged obj )
        {
            this.Invalid = true;
            //System.Threading.Thread.Sleep(10);
            //_glPanel.Invalidate();
        }

        public void SaveScreenshot(string filename, string filetype)
        {
            _realView.SaveScreenshot(filename, filetype);
        } 
    }
}
